package com.ymatou.iapi.dguser.testcase;

import java.io.IOException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.Result;

import com.ymatou.iapi.dguser.parameter.UserCreateBean;
import com.ymatou.iapi.dguser.service.UserCreateCall;
import com.ymatou.iapi.userservice.parameter.AddressRequestDtoBean;
//import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AuthCallServiceV2;
import com.ymttest.business.service.DGUserCallService;
import com.ymttest.business.service.JUserCallService;
import com.ymttest.business.service.ZhongMianStgCallService;
import com.ymttest.database.sqlwapper.DGUserWapper;
import com.ymttest.utils.TestDataManager;

@TestSuite
public class ZhongMianStgTest {
	@BeforeClass
	public static void setUp() {
//		Logger.createResultFile("ZhongMianStgTest");
	}

	@Before
	public void caseUp() {
	}

	@AfterClass
	public static void tearDown() {
//		Logger.generateResult("zhongmian");
	}

	@After
	public void caseDown() {
//		Logger.end();
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_ZhongMianStgTest_002() {
//		Logger.start(true, "领券");
		for (int i = 0; i < 20; i++) {
			try {
				TestDataManager.getLines("ZhongMianStgTest").forEach(x -> {
					String[] ss = x.split(",");
					try {
						ZhongMianStgCallService.receiveCoupon(Integer.valueOf(ss[0]), "P5MNT2RRLZ");
					} catch (NumberFormatException | IOException e) {
						e.printStackTrace();
					}
				});
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_ZhongMianStgTest_002_Thread() {
		int threadNum = 10;
		int count = 20;
		ExecutorService SERVICE = Executors.newFixedThreadPool(threadNum);
		CountDownLatch countDownLatch = new CountDownLatch(count);
		for (int i = 0; i < count; i++) {
			SERVICE.execute(() -> {
				try {
					TestDataManager.getLines("ZhongMianStgTest").forEach(x -> {
						String[] ss = x.split(",");
						try {
							ZhongMianStgCallService.receiveCoupon(Integer.valueOf(ss[0]), "P5MNT2RRLZ");
						} catch (NumberFormatException | IOException e) {
							e.printStackTrace();
						}
					});
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				countDownLatch.countDown();
			});
		}
		try {
			// 等待线程池里所有线程任务结束
			countDownLatch.await();
		} catch (Exception e) {
//			Logger.info("运行error:" + e.getStackTrace());
		} finally {
			SERVICE.shutdown();
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_ZhongMianStgTest_001() {
//		Logger.start(true, "创建买家用户成功");
		for (int i = 0; i < 1000; i++) {
			try {
				Integer uid = ZhongMianStgCallService.createUser();
				String token = ZhongMianStgCallService.createToken(uid);
				String addressid = ZhongMianStgCallService.createAddress(uid);
				TestDataManager.appendFileToLine("ZhongMianStgTest", uid + "," + token + "," + addressid);
			} catch (Exception e) {
//				Logger.fail(e);
			}
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_ZhongMianStgTest_001_Thread() {
		int threadNum = 10;
		int count = 1000;
		ExecutorService SERVICE = Executors.newFixedThreadPool(threadNum);
		CountDownLatch countDownLatch = new CountDownLatch(count);
		for (int i = 0; i < count; i++) {
			SERVICE.execute(() -> {
				try {
					Integer uid = ZhongMianStgCallService.createUser();
					String token = ZhongMianStgCallService.createToken(uid);
					String addressid = ZhongMianStgCallService.createAddress(uid);
					TestDataManager.appendFileToLine("ZhongMianStgTest", uid + "," + token + "," + addressid);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				countDownLatch.countDown();
			});
		}
		try {
			// 等待线程池里所有线程任务结束
			countDownLatch.await();
		} catch (Exception e) {
//			Logger.info("运行error:" + e.getStackTrace());
		} finally {
			SERVICE.shutdown();
		}
	}
}
